package com.api.interf;

import com.api.util.ExcelUtil;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

public class RegisterCase {
    @Test(dataProvider = "datas")
    public void test(String url,String type,String mobilephone,String pwd){
        if("post".equalsIgnoreCase(type)){
            HttpPost httpPost = new HttpPost(url);
            try {
                //表单形式构建参数名和参数值
                NameValuePair mobileponePair = new BasicNameValuePair("mobilephone",mobilephone);
                NameValuePair pwdPair = new BasicNameValuePair("pwd",pwd);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(mobileponePair);
                params.add( pwdPair);
                //将参数封装在请求体中
                httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
                //创建客户端
                CloseableHttpClient httpClient = HttpClients.createDefault();

                //发送接口请求,获取接口响应
                CloseableHttpResponse httpResponse = httpClient.execute(httpPost);

                //获取响应数据，为json格式，转换为字符串
                HttpEntity resultJson = httpResponse.getEntity();
                //将json转换为字符串
                System.out.println(resultJson);
                String resultString = EntityUtils.toString(resultJson);
                System.out.println(resultString);

            } catch (Exception e) {
                e.printStackTrace();
            }

        } else if ("get".equalsIgnoreCase(type)) {
            url+="?mobilephone="+mobilephone+"&pwd="+pwd;
            HttpGet httpGet = new HttpGet(url);
            //准备接口地址
            CloseableHttpClient httpClient = HttpClients.createDefault();
            try {

                //发送接口请求,获取接口响应
                CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
                //获取响应数据，为json格式，转换为字符串
                HttpEntity resultJson = httpResponse.getEntity();
                //将json转换为字符串
                System.out.println(resultJson);
                String resultString = EntityUtils.toString(resultJson);
                System.out.println(resultString);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        }

        //准备接口地址


    @DataProvider(name = "datas")
    public Object [][] datas(){
        String[] cellNames={"Url(接口地址)","Type(接口提交类型)","Mobilephone(手机号)","Pwd(密码)"};
        Object [][] datas = ExcelUtil.read("src/test/resources/cases_v1.xls","用例",cellNames);
        return datas;
}}
